// pages/collage/collage.js
var $ = require('../util/commit.js');
var page = 1;
var pageMore = false;
var MoveStatus = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabIndex: -1,

    FilterStatus: false, //过滤条件显示状态
    TypeSelectValId: 0,//分类选择值ID
    TypeSelectVal: 0, //分类选择值
    AdrrSelectVal: 0, //位置选择值
    DateSelectVal: 0, //日期选择值
    DateVal: '',
    PriceSelectVal: 0, //价格选择值
    TarbarData: [{
      id: '0',
      name: '分类'
    }, {
      id: '1',
      name: '位置'
    }, {
      id: '2',
      name: '日期'
    }, {
      id: '3',
      name: '价格'
    }],
    TypeData: [],
    AdrrData: [{
      id: 0,
      name: '全部'
    }, {
      id: 1,
      name: '线上活动'
    }, {
      id: 2,
      name: '2公里'
    }, {
      id: 3,
      name: '3公里'
    }, {
      id: 4,
      name: '4公里'
    }, {
      id: '5',
      name: '5公里'
    }],
    DateData: [{
      id: 0,
      name: '全部'
    }, {
      id: 1,
      name: '今天'
    }, {
      id: 2,
      name: '明天'
    }, {
      id: 3,
      name: '本周'
    }, {
      id: 4,
      name: '本周末'
    }, {
      id: 5,
      name: '本月'
    }],
    PriceData: [{
      id: 0,
      name: '全部',
    }, {
      id: 1,
      name: '免费'
    }, {
      id: 2,
      name: '付费',
    }],
    ListData: [],
    showLoading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    that.loadData();
    that.loadTypeData();
  },
  //加载
  loadData: function(e) {
    wx.showLoading({
      title: '加载中..',
    });
    let that = this;
    $.POST({
      url: 'caSCL',
      data: {
        page: 1,
        collage: that.data.TypeSelectValId,
        address: that.data.AdrrSelectVal,
        time: that.data.DateSelectVal,
        price: that.data.PriceSelectVal,
        date: that.data.DateVal
      }
    }, function(e) {
      console.log(e.data);
      that.setData({
        ListData: e.data
      });
      wx.hideLoading();
      page = 1;
      pageMore = true;
    }, function(e) {
      wx.hideLoading();
    })
  },
  // 加载更多
  loadMoreData: function(e) {
    wx.showLoading({
      title: '加载中..',
    });
    let that = this;
    if (pageMore) {
      pageMore = false;
      page++
      $.POST({
        url: 'caSCL',
        data: {
          page: page,
          collage: that.data.TypeSelectValId,
          address: that.data.AdrrSelectVal,
          time: that.data.DateSelectVal,
          price: that.data.PriceSelectVal,
          date: that.data.DateVal
        }
      }, function(e) {
        let newListData = that.data.ListData.concat(e.data);
        that.setData({
          ListData: newListData
        });
        pageMore = true;
        wx.hideLoading();
      }, function(e) {
        pageMore = true;
        wx.hideLoading();
      })
    }

  },
  // 选择开始时间
  bindPickerStartDTChange: function(e) {
    this.setData({
      DateVal: e.detail.value,
      DateSelectVal:-1
    })
    this.loadData()
  },
  // 过滤分类
  loadTypeData: function(e) {
    let that = this;
    $.POST({
      url: 'caSCC',
      data: {

      }
    }, function(e) {
      let newTypeData = [{
        collage_id: 0,
        title: '全部',
        type: 1
      }]
      newTypeData = newTypeData.concat(e.data);
      that.setData({
        TypeData: newTypeData
      })
    }, function(e) {

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      FilterStatus: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.setData({
      FilterStatus: false
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // loadMoveData
  loadMoveData: function(e) {
    let that = this;
    if (e.detail.scrollTop < -30) {
      if (!that.MoveStatus) {
        that.MoveStatus = true;
        that.loadData();
      }
    }
    if (e.detail.scrollTop == 0) {
      that.MoveStatus = false;
    }


    if (e.detail.scrollTop > 0) {
      if (e.detail.deltaY > 5) {
        if (!that.MoveStatus) {
          that.MoveStatus = true;
          that.loadMoreData();
          console.log('loadMoreData');
        }
      }
      if (e.detail.deltaY == -1 || e.detail.deltaY == 1) {
        that.MoveStatus = false;
      }
    }

  },
  //选择Tab
  selectTab: function(e) {
    var that = this;
    var tabIndex = e.currentTarget.dataset.index;
    that.setData({
      TabIndex: tabIndex,
      FilterStatus: true,
    })
  },
  // 隐藏Ta内容
  HideFilter: function(e) {
    var that = this;
    that.setData({
      FilterStatus: false,
    })
  },
  // 选择Tab内容
  TypeSelect: function(e) {
    var that = this;
    var TSelectValIndex = e.currentTarget.dataset.index;
    that.setData({
      TypeSelectVal: TSelectValIndex,
      TypeSelectValId: e.currentTarget.dataset.id,
      FilterStatus: false,
    });
    that.loadData();
  },
  // 选择位置
  AdrrSelect: function(e) {
    var that = this;
    var ASelectVal = e.currentTarget.dataset.index;
    that.setData({
      AdrrSelectVal: ASelectVal,
      FilterStatus: false,
    });
    that.loadData();
  },
  // 选择日期
  DateSelect: function(e) {
    var that = this;
    var DSelectVal = e.currentTarget.dataset.index;
    that.setData({
      DateSelectVal: DSelectVal,
      FilterStatus: false,
      DateVal: '',
    });
    that.loadData();
  },
  // 选择价格
  PriceSelect: function(e) {
    var that = this;
    var PSelectVal = e.currentTarget.dataset.index;
    that.setData({
      PriceSelectVal: PSelectVal,
      FilterStatus: false,
    });
    that.loadData();
  },
  //跳转详细页
  ToCollageDetail: function(e) {
    $.openWin({
      url: '../collageDetail/collageDetail',
      data: {
        id: e.currentTarget.dataset.id,
      }
    })
  },
  // TopJoinPsn
  TopJoinPsn: function(e) {
    $.openWin({
      url: '../joinPsnPage/joinPsnPage',
      data: {
        id: e.currentTarget.dataset.id,
      }
    })
  },
  // 发布活动
  ToPublishPage: function(e) {
    $.openWin({
      url: '../publishActivitie/publishActivitie',
    })
  },
  formSubmit: function(e) {
    $.POST({
      url: 'caUserAFI',
      data: {
        formid: e.detail.formId,
      }
    }, function(e) {

    }, function() {

    })
  }

})